
angular.module('app').controller('integralOrderCtrl',
    ['$scope', '$rootScope', '$http', '$stateParams', 'utils', 'activity',
        function ($scope, $rootScope, $http, $stateParams,utils, activity) {
            $scope.totalList; //总的列表
            $scope.notdeliveredList;//未发货
            $scope.deliveredList;//发货
            $scope.slectType=0;
            $scope.goback = function() {
                window.history.go(-1);
            };
            $scope.isShow=false;
            /**
             * 筛选
             */
           $('.order_filter').find('li').on('click',function (event) {
               $('.order_filter').find('li').removeClass('active');
               $(event.target).addClass('active');
               $scope.isShow=!$scope.isShow;
               var index=$(".order_filter  li").index(this);
               switch (index){
                   case 0:
                       $scope.integralOrderList=$scope.totalList;
                       $scope.slectType=0;
                       break;
                   case 1:
                       $scope.integralOrderList=$scope.notdeliveredList;
                       $scope.slectType=1;
                       break;
                   case 2:
                       $scope.integralOrderList=$scope.deliveredList;
                       $scope.slectType=2;
                       break;
               }
               mui('#pullrefresh').pullRefresh().scrollTo(0,0,100);
               if($rootScope.isapp) {
                   wv.setHeader('我的订单','1','筛选','3')
               }

               $scope.$apply();
           });

            /**
             * 获取时间数据
             * @param tradeTime
             * @returns {string}
             */
            $scope.getdate=function (tradeTime) {
                var date = new Date(tradeTime);
                var year = date.getFullYear();
                var month = date.getMonth()+1;    //js从0开始取
                var day = date.getDate();
                var hour = date.getHours();
                var minutes = date.getMinutes();
                var second = date.getSeconds();
                if(month<10){
                    month = "0" + month;
                }
                if(day<10){
                    day = "0" + day;
                }
                if(hour <10){
                    hour = "0" + hour;
                }
                if(minutes <10){
                    minutes = "0" + minutes;
                }
                if(second <10){
                    second = "0" + second ;
                }
               return year+'-'+month+'-'+day+'  '+hour+':'+minutes+":"+second;
            };

            /**/
            /**
             * 获取订单
             * @param res
             */
            function getOrderList(page) {
                $scope.page=1;
                $http.get(utils.integralSys, {
                    params: {
                        header: utils.header("300204"),
                        body: {
                            userCode:activity.getSoloCode(),//	用户代码，不能为空
                            logisticsState:'',
                            page:page,
                            rows:'40',
                        }
                    }
                }).then(function (res) {
                    if(res.header.re_code =="0000") {
                        rendData(res.body.rows);
                    }
                        if($rootScope.isapp) {
                            wv.setHeader('我的订单','1','筛选','3')
                        }
                    mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                    mui('#pullrefresh').pullRefresh().refresh(true);
                    if(!res.body.rows.length > 0){
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                        mui('#pullrefresh').pullRefresh().destroy();

                    }
                })
            }

            /**
             * 更新数据
             * @param res
             */
           function rendData(res) {
                $scope.totalList = res;
                switch ($scope.slectType) {
                    case 0:
                        $scope.integralOrderList = res;
                        $scope.notdeliveredList = listSelect($scope.totalList, 1);
                        $scope.deliveredList = listSelect($scope.totalList, 2);
                        break
                    case 1:
                        $scope.notdeliveredList = listSelect($scope.totalList, 1);
                        $scope.deliveredList = listSelect($scope.totalList, 2);
                        $scope.integralOrderList = $scope.notdeliveredList;
                        break
                    case 2:
                        $scope.notdeliveredList = listSelect($scope.totalList, 1);
                        $scope.deliveredList = listSelect($scope.totalList, 2);
                        $scope.integralOrderList = $scope.deliveredList;
                        break
                }

            }

            if($scope.isapp){
                appload={
                    CallBack:function (type) {
                        $scope.isShow=!$scope.isShow;
                        if(type=='3'){
                                if($rootScope.isapp) {
                                    wv.setHeader('我的订单','1','收起','4')
                                }
                        }else if(type=='4'){
                                if($rootScope.isapp) {
                                    wv.setHeader('我的订单','1','筛选','3')
                                }
                        }
                        $scope.$apply();
                    }
                };
            }
            tap();
            /**
             * 添加点击时间
             */
            function tap() {
              mui('.orderList').off('tap','.tiger');
              setTimeout(function () {
                  mui('.orderList').on('tap','.tiger',function (event) {
                      var  itemAddress= $(this).data("address");
                      var  itemOrderId= $(this).data("orderid");
                      if(itemAddress){
                          var path = $(event.target).attr('src');
                          if(path=='image/integral/ic_down.png'){
                              $(event.target).attr('src','image/integral/ic_up.png');
                              for (var i = 0; i < $scope.integralOrderList.length; i++) {
                                  var obj = $scope.integralOrderList[i];
                                  if(obj.orderId==itemOrderId){
                                      obj.isShow=true;
                                  }
                              }
                          }else {
                              $(event.target).attr('src','image/integral/ic_down.png');
                              for (var i = 0; i < $scope.integralOrderList.length; i++) {
                                  var obj = $scope.integralOrderList[i];
                                  if(obj.orderId==itemOrderId){
                                      obj.isShow=false;
                                  }
                              }
                          }
                          $scope.$apply();
                      }
                  });
              },600);
         }



            $scope.showExpressdelivery=function (item) {
               if(item.waybill){
                   return true;
               }else {
                   return false
               }
            }


            function listSelect(res,type) {
                var typeList=new Array();
                if(res&&res.length>0){
                    for (var i = 0; i < res.length; i++) {
                        var obj = res[i];
                        if(type==1&&obj.logisticsState=='未发货'){
                            typeList.push(obj)
                        }else if(type==2&&obj.logisticsState=='已发货') {
                            typeList.push(obj)
                        }
                    }
                }
                return typeList;
            }


            $scope.$on('$viewContentLoaded', function(event) {
                var oh = document.documentElement.clientHeight
                var ow = document.documentElement.clientWidth
                var bh = document.documentElement.clientWidth
                /*设置滚动区域高度*/
                $("#pullrefresh").height((oh) + 'px')
                $("#empty").height((bh) + 'px').width(ow + 'px');
                 setPullRefresh(mui("#pullrefresh"));
               $(".mui-pull-top-pocket").css('top','20px');
                $(".mui-pull-bottom-pocket").css('margin-bottom','100px')
            });

            function setPullRefresh (element) {
                element.pullRefresh({
                    down: {
                        height: 50,
                        callback: pulldownRefresh,
                        auto: true
                    },
                    up: {
                        height: 50, //可选.默认50.触发上拉加载拖动距离
                        auto: true, //可选,默认false.自动上拉加载一次
                        contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
                        contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
                        callback: pullfresh //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                    }
                })

            }

            /*下拉刷新*/
            function pulldownRefresh() {
                getOrderList(1);
            }

            /*上拉加载*/
            function pullfresh() {
                $scope.page=$scope.page+1;
                $http.get(utils.integralSys, {
                    params: {
                        header: utils.header("300204"),
                        body: {
                            userCode:activity.getSoloCode(),//	用户代码，不能为空
                            logisticsState:'',
                            page:$scope.page,
                            rows:'40',
                            order:'desc',
                        }
                    }
                }).then(function (res) {
                    if(res.header.re_code =="0000") {
                            rendData($scope.totalList.concat(res.body.rows));
                        if(res.body.rows.length > 0){
                            mui('#pullrefresh').pullRefresh().endPullupToRefresh(false)
                        }else {
                            mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                        }
                    }
                });
                mui('#pullrefresh').pullRefresh().endPulldownToRefresh();

            }

        }])